<?php
class Poll_Widget_RandomQuestionsController extends Engine_Content_Widget_Abstract
{ 
    public function indexAction() {
        $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
        $params['privacy'] = 'all';
        $params['viewer'] = $viewer->getIdentity();
        $params['order'] = 'RAND()';
        $sl = Engine_Api::_()->poll()->pollSelect($params);
        $tmpSL = Engine_Api::_()->getDbTable('Polls', 'Poll')->select()
            ->distinct()
            ->from(array('q' => 'engine4_poll_polls'), array('poll_id'))
            ->join(array('o' => 'engine4_poll_options'), 'q.poll_id = o.poll_id', NULL)
            ->join(array('v' => 'engine4_poll_votes'), 'o.poll_option_id = v.option_id', NULL)
            ->where('v.user_id = ?', $viewer->getIdentity());
        $sl->where("poll_id NOT IN ({$tmpSL->__toString()})");
        
        $this->view->paginator = $paginator = Zend_Paginator::factory($sl);
        $paginator->setItemCountPerPage($this->_getParam('limit', 5));
        if (!count($paginator)) return $this->setNoRender(true);
    }
}
